//index.js
const app = getApp()

Page({
  data: {
    imgUrls: [],
    autoplay: true,
    interval: 3000,
    duration: 1000,
    motto: 'Hello World',
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    isPhoneX: true
  },
  bindViewTap: function () {
    this.setUser();
    // 注册
    this.onRegister();
    wx.switchTab({
      url: '../show/show',
    })
  },
  onLoad: function (options) {
    var that = this;
    this.setData({
      userInfo: {},
      hasUserInfo: false,
    });

    let j = options.userInfo;
    wx.getSystemInfo({
      success: function (res) {
        var model = res.model;
        var iphoneX = 'iPhone X';
        var iphone6 = 'iPhone 6';
        var modeltest = /^iPhone 6/;
        var isMobile = modeltest.exec(model);

        if (isMobile) {
          if (isMobile[0] == iphone6) {
            that.setData({
              isPhoneX: false
            });
          }
        }

      },
      fail: function (res) {
      },
      complete: function (res) {
      },
    })
  },

  onShow: function () {
    wx.hideTabBar();
    this.setData({
      userInfo: {},
      hasUserInfo: false
    })
  },

  setUser: function () {

    this.setData({
      userInfo: null,
    })

    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      })
    } else if (this.data.canIUse) {
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo
          this.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
        }
      })
    }
  },

  //注册用户信息
  onRegister: function () {
    var id = wx.getStorageSync("openId");
    var userInfo = wx.getStorageSync("userInfo");
    var image = userInfo.avatarUrl;
    var city = userInfo.city;
    var country = userInfo.country;
    var name = userInfo.nickName;
    var sex = userInfo.gender;
    wx.request({
      method: 'post',
      header: {
        "Content-Type": "application/x-www-form-urlencoded",
        "X-Token": id,
      },
      url: app.globalData.siteUrl +'/user/register',
      data: {
        //注册数据
        id: id,
        image: image,
        city: city,
        country: country,
        nickName: name,
        name: name,
        sex: sex
      },
      success: function (result) {
      }
    })
  },

  getUserInfo: function (e) {
    // 登录
    var that = this;
    wx.login({
      success: function (res) {
        if (res.code) {  //wx.login获取code。
          //发起网络请求
          wx.request({
            url: app.globalData.siteUrl +'/user/login',
            data: {
              code: res.code,   //将code发送到后台服务器。
            },
            success: function (result) {
              app.globalData.openId = result.data.model.token
              wx.setStorageSync("openId", result.data.model.token);
            }
          })
        } else {
        }
      }
    });
    // 获取用户信息
    wx.getSetting({
      success: res => {
        if (res.authSetting['scope.userInfo']) {
          // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
          wx.getUserInfo({
            success: res => {
              // 可以将 res 发送给后台解码出 unionId
              app.globalData.userInfo = res.userInfo
              wx.setStorageSync("userInfo", res.userInfo);
              this.setData({
                userInfo: app.globalData.userInfo,
                hasUserInfo: true
              })
              // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
              // 所以此处加入 callback 以防止这种情况
              if (this.userInfoReadyCallback) {
                this.userInfoReadyCallback(res)
              }
            }
          })
        }
      }
    });
  }

})
